<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片试图区域 -->
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入搜索内容" clearable>
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单列表数据 -->
      <el-table :data="orderList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column label="订单价格" prop="order_price"></el-table-column>
        <el-table-column label="是否付款" prop="order_status">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.pay_status === '1'"
              >已付款</el-tag
            >
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send">
          <template slot-scope="scope"> {{ scope.row.is_send }}</template>
        </el-table-column>
        <el-table-column label="下单时间" prop="create_time">
          <template slot-scope="scope">
            {{ scope.row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="editClicked(scope.row)"
            ></el-button>
            <el-button
              type="success"
              size="mini"
              icon="el-icon-location"
              @click="progressClicked(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 15, 20, 25]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 地址修改弹窗 -->
    <el-dialog title="提示" :visible.sync="orderDialogVisible" width="50%">
      <el-form
        :model="orderForm"
        :rules="orderFormRules"
        ref="orderFormRef"
        label-width="100px"
      >
        <el-form-item label="订单价格" prop="order_price">
          <el-input v-model="orderForm.order_price" type="number"></el-input>
        </el-form-item>
        <el-form-item label="是否付款" prop="pay_status">
          <el-radio
            v-model="orderForm.pay_status"
            label="1"
            @change="payStateChange"
            >已付款</el-radio
          >
          <el-radio
            v-model="orderForm.pay_status"
            label="0"
            @change="payStateChange"
            >未付款</el-radio
          >
        </el-form-item>
        <el-form-item label="是否发货" prop="is_send">
          <el-radio v-model="orderForm.is_send" label="是" @change="sendChange"
            >已发货</el-radio
          >
          <el-radio v-model="orderForm.is_send" label="否" @change="sendChange"
            >未发货</el-radio
          >
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="orderDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="orderChangeClicked">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 物流进度提示框 -->
    <el-dialog
      title="物流进度"
      :visible.sync="progressDialogVisible"
      width="50%"
    >
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in progressInfo"
          :key="index"
          :timestamp="activity.time"
        >
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
      <span slot="footer" class="dialog-footer">
        <el-button @click="progressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="progressDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import cityData from './citydata'
export default {
  data () {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      orderList: [],
      orderDialogVisible: false,
      orderForm: {
        order_price: '',
        pay_status: '0',
        is_send: '否',
        user_id: 0
      },
      // 订单id
      orderId: '',
      orderFormRules: {
        order_price: [{ required: true, message: '请选择省市区', trigger: 'blur' }],
        pay_status: [{ required: true, message: '请输入详细地址', trigger: 'blur' }],
        is_send: [{ required: true, message: '请输入详细地址', trigger: 'blur' }]
      },
      cityData,
      // 物流进度弹窗默认关闭
      progressDialogVisible: false,
      // 物流进度data
      progressInfo: [
        {
          time: '2018-05-10 10:00:43',
          ftime: '2018-05-10 12:20:17',
          context: '已签收，感谢使用顺丰快递',
          location: ''
        },
        {
          time: '2018-05-09 06:23:42',
          ftime: '2018-05-09 08:21:32',
          context: '北京大望路代收点已揽件',
          location: ''
        },
        {
          time: '2018-05-18 13:25:31',
          ftime: '2018-05-18 13:27:52',
          context: '湖南武汉崔各庄代收点已揽收',
          location: ''
        },
        {
          time: '2018-05-18 08:23:21',
          ftime: '2018-05-18 08:46:27',
          context: '湖南杨庄已发货',
          location: ''
        }
      ]
    }
  },
  created () {
    this.getOrderList()
  },
  methods: {
    async getOrderList () {
      const { data: res } = await this.$http.get('orders', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单列表失败！')
      }
      this.total = res.data.total
      this.orderList = res.data.goods
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },
    // 修改点击事件
    editClicked (row) {
      console.log(row)
      this.orderId = row.order_id
      this.orderDialogVisible = true
      this.orderForm.order_price = row.order_price
      this.orderForm.pay_status = row.pay_status
      this.orderForm.is_send = row.is_send
      this.orderForm.user_id = row.user_id
    },
    payStateChange (value) {
      this.orderForm.pay_status = value
    },
    sendChange (value) {
      this.orderForm.is_send = value
    },
    // 修改订单提交
    orderChangeClicked () {
      this.$refs.orderFormRef.validate(async valid => {
        if (!valid) {
          return this.$message.error('请填写完整表单信息！')
        } else {
          const { data: res } = await this.$http.put(`orders/${this.order_id}`, this.orderForm)
          console.log(res)
          if (res.meta.status !== 200) {
            return this.$message.error('订单信息修改失败！')
          }
          this.$message.success('订单信息修改成功！')
          this.orderDialogVisible = false
          this.getOrderList()
        }
      })
    },
    // 物流进度点击事件（这里progressId只能用视频提供的测试Id）
    async progressClicked (rowData) { // 接口不能用只能写死数据展示
      //   const { data: res } = await this.$http.get('/kuaidi/804909574412544580')
      //   console.log(res)
      //   if (res.meta.status !== 200) {
      //     return this.$message.error('获取物流进度失败！')
      //   }
      //   this.progressInfo = this.data
      this.progressDialogVisible = true
    }
  }
}
</script>

<style lang="less" scoped>
</style>
